package com.dbappsecurity.bifrost.receiver;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.TextUtils;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
import com.dbappsecurity.bifrost.R;
import com.dbappsecurity.bifrost.view.MainActivity;
import com.google.gson.Gson;

import static android.support.constraint.Constraints.TAG;
import static android.support.v4.app.NotificationCompat.DEFAULT_SOUND;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;

public class MyReceiver extends BroadcastReceiver {
  public static int badgeCount = 0;

  @Override
  public void onReceive(Context context, Intent intent) {
    try {
      Bundle bundle = intent.getExtras();
      String extra = printBundle(bundle);
      Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + extra);
      if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
        String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
        //Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
        //send the Registration Id to your server...

      } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
        //Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
      } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
        //Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
        int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
        //Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

        //收到通知
        notificationReceived(context, extra);
      } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
        //Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
      } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
        //Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(
        //    JPushInterface.EXTRA_EXTRA));
        //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

      } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
        boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
        //Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
      } else {
        //Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
      }
    } catch (Exception e) {

    }
  }

  public void notificationReceived(final Context context, String extra) {

    Gson gson = new Gson();
    //
    Extra content = gson.fromJson(extra, Extra.class);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context, context.getResources().getString(R.string.app_name))
            .setSmallIcon(R.drawable.icon_app)
            .setDefaults(DEFAULT_SOUND)
            .setAutoCancel(true)
            .setPriority(PRIORITY_HIGH)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setContentText(content.content);
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(10029, mBuilder.build());
  }

  public static class Extra {
    public String content;
    public String url;
  }

  private static String printBundle(Bundle bundle) {
    StringBuilder sb = new StringBuilder();
    for (String key : bundle.keySet()) {
      if (key.equals(JPushInterface.EXTRA_EXTRA)) {
        if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
          //Log.i(TAG, "This message has no Extra data");
          continue;
        }

        sb.append(bundle.getString(JPushInterface.EXTRA_EXTRA));
      }
    }
    return sb.toString();
  }
}
